package com.yizhang.pattern.chain;

/**
 * @Author: yi.zhang
 * @CreateTime: 2025-08-22 15:41:54
 * @Description:
 */
public abstract class Handler<T> {
    protected Handler<T> next;

    /**
     * 执行链
     * @param next
     */
    public void setNext(Handler<T> next) {
        this.next = next;
    }

    /**
     * 执行逻辑
     * @param t
     */
    public abstract void handle(T t);

    /**
     * 建造者模式
     */
    public static class Builder<T> {
        private Handler<T> head;
        private Handler<T> tail;

        public Builder<T> addHandler(Handler<T> handler) {
            if (this.head == null) {
                this.head = this.tail = handler;
                return this;
            }
            this.tail.next = handler;
            this.tail = this.tail.next;
            return this;
        }
        public Handler<T> build() {
            return this.head;
        }
    }
}
